package com.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.UserAccountDao;
import com.enetiy.Useraccount;

public class UseraccountDaoImpl extends HibernateDaoSupport implements
		UserAccountDao {

	@Override
	public Useraccount selectUseraccount(String account) throws Exception {
		Query query = this.getSession().createQuery("from Useraccount where account=?");
		query.setParameter(0, account);
		List <Useraccount>list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public void insetrUseraccount(Useraccount ua) throws Exception {
		this.getHibernateTemplate().save(ua);
	}
	@Override
	public List<Useraccount> selectUseraccounts(int userid,int type) throws Exception {
		Query query = this.getSession().createQuery("from Useraccount where users_id=? and accountype=?");
		query.setParameter(0, userid);
		query.setParameter(1, type);
		List <Useraccount>list = query.list();
		return list;
	}

	@Override
	public void updateUseraccount(Useraccount ua) throws Exception {
		this.getHibernateTemplate().update(ua);
		
	}

	@Override
	public Useraccount selectUseraccount(int userid, int type) throws Exception {
		Query query = this.getSession().createQuery("from Useraccount where users_id=? and accountype=?");
		query.setParameter(0, userid);
		query.setParameter(1, type);
		List <Useraccount> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public Useraccount selUseraccountOpenId(String openid) throws Exception {
		Query query = this.getSession().createQuery("from Useraccount where weixin_open=? and status =1");
		query.setParameter(0, openid);
		List <Useraccount>list = query.list();
		return list.isEmpty()?null:list.get(0);
	}
	
}
